package com.mxpio.erp.common.jpa.policy;

import com.mxpio.erp.common.jpa.entity.BizNoticeType;
import com.mxpioframework.common.util.BeanReflectionUtils;
import com.mxpioframework.common.util.SpringUtil;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.policy.impl.AbstractGeneratorPolicy;
import com.mxpioframework.jpa.policy.impl.CrudType;
import com.mxpioframework.system.service.SnRuleService;

public class NoticeCodeGeneratorPolicy extends AbstractGeneratorPolicy {

	@Override
	public CrudType getType() {
		return CrudType.SAVE;
	}

	@Override
	protected Object getValue(Object entity, String field) {
		Object value = BeanReflectionUtils.getPropertyValue(entity, field);
		Object typeCode = BeanReflectionUtils.getPropertyValue(entity, "typeCode");
		if(value == null || "".equals(value)){
			if(typeCode == null){
				value = null;
			}else{
				BizNoticeType bizNoticeType = JpaUtil.linq(BizNoticeType.class).idEqual(typeCode).findOne();
				StringBuffer expression = new StringBuffer(bizNoticeType.getTypePrefix());
				for(int i=0 ;i < bizNoticeType.getTypeSize(); i++){
					expression.append("#");
				}
				SnRuleService snRuleService = SpringUtil.getBean(SnRuleService.class);
				value = snRuleService.execute(expression.toString(), null);
			}
		}
		
		return value;
	}

}
